// pages/cinema/cinema.js
const db = wx.cloud.database()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  cinemas:'',
  movieId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },
  async onShow() {
    let pages = getCurrentPages()
    // 数组中索引最大的页面--当前页面  
    let currentPage = pages[pages.length-1]
    var options = currentPage.options
    if (typeof(options.id) !== 'undefined') {
      db.collection('movies').where({
        _id: options.id
      }).get().then(res => {
        this.setData({
          title: res.data[0].title,
          id: res.data[0]._id,
          poster:res.data[0].poster,
          duration:res.data[0].duration,
          time:res.data[0].time,
          year:res.data[0].year,
          type:res.data[0].type,
          translation:res.data[0].translation,
          language:res.data[0].language
        });
      }); 
      const app = getApp()
      var movieId = options.id
      var sessions = await app.getTimeRightSession(movieId)
      var cinemaIds = sessions.map(session_info => session_info.cinemaId); 
      this.setData({
        movieId:movieId
      })
      //根据电影院id将cinema集合里的电影院信息显示到页面上
     await db.collection('cinema').where({
        _id: db.command.in(cinemaIds)
      }).get().then(res => {
        const cinemas = res.data;
        // 将cinemas数据绑定到页面的data中，以便在页面上显示
        this.setData({
          cinemas: cinemas
        });
        console.log(cinemas)
      }).catch(err => {
        console.error(err);
      });
    }
  },
  toGoupiao:function(event){
    const cinemaId = event.currentTarget.dataset._id;
      const movieId = this.data.movieId
      console.log(cinemaId)
      console.log(movieId)
      wx.navigateTo({
        url: `/pages/dingpiao/dingpiao?cinemaId=${cinemaId}&movieId=${movieId}`
      })
    },
   
})